///<reference path='./index.d.ts'/>
import store from "store2"

/**存储数据到localStorage */
export default class CddStore {
  [ key: string ]: Entire

  static minute = 1000 * 60
  static hour = 60 * CddStore.minute
  static day = 24 * CddStore.hour

  constructor (params: {key: string, value: any, refresh?: Function, days?: number, minutes?: number, hours?: number, forever: false, aotuRefresh?: boolean, autorenewal?: boolean}[]) {

    if (params && params.length > 0) {
      for (let item of params) {
        // 设置时效
        // 时效，默认半天
        let expires = 0
        expires += item.days !== undefined ? item.days * CddStore.day : 0
        expires += item.minutes !== undefined ? item.minutes * CddStore.minute : 0
        expires += item.hours !== undefined ? item.hours * CddStore.hour : 0
        if (item.forever) {
          expires = 2 * 365 * CddStore.day
        } else {
          expires === 0 && (expires = CddStore.day * .5)
        }
        this[ item.key ] = new Entire({expires, ...item})
      }
    }
  }
}

/**存储数据格式类 */
class Entire {
  key: string
  private _value: any
  expires: string | number
  stamp = Date.now()
  aotuRefresh = true
  autorenewal = false
  refresh?: Function

  constructor (params: {key: string, value: any, expires: string | number, refresh?: Function, aotuRefresh?: boolean, autorenewal?: boolean, [ key: string ]: any}) {

    // 如果存在则进行赋值
    const data = store.get(params.key);
    if (!params.value && data) {
      if (Date.now() - data.stamp < data.expires) {
        params.value = data._value;
      }
    }

    this.key = params.key
    if (!params.value && params.refresh) {//默认赋值
      params.value = (params.refresh as Function)()
    }
    params.refresh && (this.refresh = params.refresh)
    this._value = params.value
    this.expires = params.expires
    params.autorenewal !== undefined && (this.autorenewal = params.autorenewal)
    params.aotuRefresh !== undefined && (this.aotuRefresh = params.aotuRefresh)

    store.set(params.key, this)
  }

  /**值 */
  get value () {
    if (this.isOver) {
      store.remove(this.key)
      if (this.autorenewal && !this.refresh) {
        this.setNewExpires()
      } else if (this.aotuRefresh && this.refresh) {
        this.setValue(this.refresh())
        return this._value
      } else {
        console.log(`${this.key}已过期`)
        return undefined
      }
    }
    return this._value
  }

  /**获取值 */
  getValue () {
    if (this.isOver) {
      store.remove(this.key)
      if (this.autorenewal && !this.refresh) {
        this.setNewExpires()
      } else if (this.aotuRefresh && this.refresh) {
        this.setValue(this.refresh())
        return this._value
      } else {
        console.log(`${this.key}已过期`)
        return undefined
      }
    }
    return this._value
  }
  /**设置值*/
  setValue (value: any) {
    this._value = value
    this.setNewExpires()
  }


  /**重写设置延续时间 */
  setNewExpires (duration?: number) {
    this.stamp = Date.now()
    this.expires = duration || this.expires
    store.set(this.key, this)
  }
  /**延期*/
  renewal () {
    if (this.refresh) {
      this._value = this.refresh()
    }
    this.setNewExpires()
  }
  /**是否超时 */
  get isOver () {
    return this._value !== undefined && Date.now() - this.stamp >= this.expires
  }
}
